home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17121 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: prairienet.org!wemccaug
  2. From: wemccaug@prairienet.org (Wendy E. McCaughrin)
  3. Newsgroups: comp.lang.c++
  4. Subject: RE: Proper use of friend keyword
  5. Date: 13 Apr 1996 22:55:33 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4kpbd5$spq@vixen.cso.uiuc.edu>
  8. References: <00001a81+0000b164@msn.com> <4jn38u$j9c@holly.ACNS.ColoState.EDU>
  9. Reply-To: wemccaug@prairienet.org (Wendy E. McCaughrin)
  10. NNTP-Posting-Host: firefly.prairienet.org
  11.  
  12.  
  13. Reply-To: wemccaug@prairienet.org
  14.  
  15. In a previous article, Steve_Quist@msn.com (Stephen Quist) says:
  16.  
  17. >Corby Hudnall wrote:
  18. >
  19. >Hey all, I have a question about how to use the friend keyword.  Consider
  20. >the following example:
  21. >
  22. >class.h---------
  23. >class ABC
  24. >{
  25. >    int AValue;
  26. >    int GetAValue() { return (AValue); }
  27. >public:
  28. >    ABC() { AValue=5; }
  29. >    ~ABC(){ }
  30. >};
  31. >
  32. >class XYZ
  33. >{
  34. >public:
  35. >    XYZ() { }
  36. >    ~XYZ() { }
  37. >    friend int ABC::GetAValue();
  38. >};
  39. >
  40. >prog.C--------------------
  41. >#include <iostream.h>
  42. >#include "class.h"
  43. >
  44. >void main()
  45. >{
  46. >  ABC abc;
  47. >  XYZ xyz;
  48. >
  49. >  cout << xyz.GetAValue() << endl
  50. >}
  51. >
  52. >
  53. >when I try to compile this, I get the message "no member funciton 
  54. >'XYZ::GetAValue()' defined."  What do I need to do in order to 
  55. >make this work.  Thanks for any and all suggestions.
  56. >-----------------
  57. >Well, the compiler is of course right. I am not sure what you are
  58. >trying to do. If you are trying to make GetAValue() a mf of XYZ
  59. >then ABC and XYZ should have an inheritance relationship. 
  60.  
  61.  What??? This has nothing to do with inheritance whatsoever. The
  62.  compiler only rejected the statement because the XYZ object xyz
  63.  contains no member function named GetAValue(). Furthermore, this
  64.  is not responsive to the author's question, which is about how
  65.  to use the 'friend' keyword. Its use was legal but inappropriate
  66.  since GetAValue() never accesses XYZ's data members (in fact, there
  67.  are none to access). The right answer to that question is:
  68.  
  69.  1. Give XYZ some data members to access and then redo
  70.     GetAValue() to access them in its code body;
  71.  
  72.  2. Since GetAValue() _is_ a member of the ABC object abc,
  73.     redo the cout to contain a call to abc.GetAValue().
  74.  
  75.  Finally, some sort of forward declaration is needed for GetAValue()
  76.  to access the as-yet-undelcared class XYZ's data member(s). Best
  77.  way is to reverse the order of delcarations, using the 'friend'
  78.  declaration as the forward reference to class ABC.
  79.